﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel.DataAnnotations;

namespace Domain.Test
{
    [TestClass]
    public class UnitTestCustomer
    {
        [TestMethod]
        public void WhenValidatingCustomerWithMissingFirstName_ResultShouldContainOneValidationErrorMessage()
        {
            //using data annotations for validation outside of a UI framework, like ASP.NET MVC .

            Customer cut = new Customer();

            
            
            var context = new ValidationContext(cut, serviceProvider: null, items: null);
            var results = new List<ValidationResult>();
            
            // a static Validator class to execute the validation rules
            var isValid = Validator.TryValidateObject(cut, context, results, true);

            Assert.IsFalse(isValid);
            Assert.IsNotNull(results);
            Assert.IsTrue(results.Count == 1);

            if (!isValid)
            {
                foreach (var validationResult in results)
                {
                    Console.WriteLine(validationResult.ErrorMessage);
                }
            }

        }


        [TestMethod]
        public void WhenValidatingCompleteCustomer_ResultShouldContainNoValidationErrorMessage()
        {
            //using data annotations for validation outside of a UI framework, like ASP.NET MVC .

            Customer cut = new Customer() {  FirstName="me"};



            var context = new ValidationContext(cut, serviceProvider: null, items: null);
            var results = new List<ValidationResult>();

            // a static Validator class to execute the validation rules
            var isValid = Validator.TryValidateObject(cut, context, results, true);

            Assert.IsTrue(isValid);
            Assert.IsNotNull(results);
            Assert.IsTrue(results.Count == 0);

           

        }


        [TestMethod]
        public void WhenValidatingCompleteCustomerWithInvalidEmail_ResultShouldContainValidationErrorMessage()
        {
            //using data annotations for validation outside of a UI framework, like ASP.NET MVC .

            Customer cut = new Customer() { FirstName = "me" , Email="***$$***$$"};



            var context = new ValidationContext(cut, serviceProvider: null, items: null);
            
           var results = new List<ValidationResult>();

            // a static Validator class to execute the validation rules
            var isValid = Validator.TryValidateObject(cut, context, results ,true);

            Assert.IsFalse(isValid);
            Assert.IsNotNull(results);
            Assert.IsTrue(results.Count == 1);



        }


        [TestMethod]
        public void WhenValidatingCompleteCustomerWithValidEmail_ResultShouldContainNoValidationErrorMessage()
        {
            //using data annotations for validation outside of a UI framework, like ASP.NET MVC .

            Customer cut = new Customer() { FirstName = "me", Email = "alexander@nowak.be" };



            var context = new ValidationContext(cut, serviceProvider: null, items: null);

            var results = new List<ValidationResult>();

            // a static Validator class to execute the validation rules
            var isValid = Validator.TryValidateObject(cut, context, results, true);

            Assert.IsTrue(isValid);
            Assert.IsNotNull(results);
            Assert.IsTrue(results.Count == 0);



        }


    }
}
